@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('css/jquery.form.css')}}" type="text/css" rel="stylesheet" />
    <!-- 配置文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.config.js')}}"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.all.js')}}"></script>
@endsection

@section('html-body')
        <!-- content start -->
    <div id="content">
        <div class="gg-step">
            <form  id="advform">
                <input type="hidden" name="id" value=""/>
                <div class="gg-list">
                    <label class="tit">商品名称</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入标题" name="title" id="name" value="@if($good){{$good->name}} @endif" /><cite class="required">*</cite>
                    </div>
                </div>

                <div class="gg-list" data-id="ggzst2">
                    <label class="tit">展示图</label>
                    <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;">
                        <!--  <input type="file"><br/> -->
                        <input type = "hidden" name = "pic_img" id = "pic_img2" value = "" height="70px" >
                        <div id="showimg">
                        </div>
                        <div class="btn" style="background: #00A2D4">
                            <span id="inputImage1">添加图片</span>
                            <input id="fileupload" type="hidden" name="thumb_id" value="@if($good){{$good->thumb_id}} @endif">
                        </div>
                        <div class="progress">
                            <span class="bar"></span><span class="percent">0%</span >
                        </div>
                        <p class="text" id="uploadthumb1">@if($good)图片id：{{$good->thumb_id}} @else展示缩略图（图片格式为jpg或png）@endif</p>
                        <!--<div class="files"></div>-->
                    </div>
                </div>

                <div class="gg-list" data-id="ggzst2">
                    <label class="tit">缩略图1</label>
                    <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;">
                        <!--  <input type="file"><br/> -->
                        {{--<input type = "hidden" name = "pic_img" id = "pic_img2" value = "" height="70px" >--}}
                        <div id="showimg">
                        </div>
                        <div class="btn" style="background: #00A2D4">
                            <span id="inputImage2">添加图片</span>
                            <input id="thumb1" type="hidden" name="thumb_id" value="@if($good){{$good->img1}} @endif">
                        </div>
                        <div class="progress">
                            <span class="bar"></span><span class="percent">0%</span >
                        </div>
                        <p class="text" id="uploadthumb2">@if($good)图片id：{{$good->img1}} @else展示缩略图（图片格式为jpg或png）@endif</p>
                        <!--<div class="files"></div>-->
                    </div>
                </div>

                <div class="gg-list" data-id="ggzst2">
                    <label class="tit">缩略图2</label>
                    <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;">
                        <!--  <input type="file"><br/> -->
                        {{--<input type = "hidden" name = "pic_img" id = "pic_img2" value = "" height="70px" >--}}
                        <div id="showimg">
                        </div>
                        <div class="btn" style="background: #00A2D4">
                            <span id="inputImage3">添加图片</span>
                            <input id="thumb2" type="hidden" name="thumb_id" value="@if($good){{$good->img2}} @endif">
                        </div>
                        <div class="progress">
                            <span class="bar"></span><span class="percent">0%</span >
                        </div>
                        <p class="text" id="uploadthumb3">@if($good)图片id：{{$good->img2}} @else展示缩略图（图片格式为jpg或png）@endif</p>
                        <!--<div class="files"></div>-->
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">市场价</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入标题" name="m_price" id="m_price" value="@if($good){{$good->m_price}} @endif" /><cite class="required">*</cite>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">实际价</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入标题" name="s_price" id="s_price" value="@if($good){{$good->s_price}} @endif" /><cite class="required">*</cite>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">商品库存</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入标题" name="stock" id="stock" value="@if($good){{$good->stock}} @endif" /><cite class="required">*</cite>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">快递费用</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入标题" name="express" id="express" value="@if($good){{$good->express_fee}} @endif" /><cite class="required">*</cite>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">文章内容</label>
                    <div class="cont">
                        <textarea class="textarea" placeholder="请输入活动备注" name="adtips" id="adtips" style="border: none"></textarea>
                    </div>
                </div>

            </form>
            <div class="gg-list" >
                <label class="tit">&nbsp;</label>
                <div class="cont" >
                    <a href="javascript:;" class="next-step" id="nextid">提交</a>
                    <input type="hidden" id="goodid" value="@if($good){{$good->id}} @else 0 @endif">
                </div>
            </div>
        </div>
    </div>
    <!-- content end -->

    <script>

        $(function() {
            <!-- 实例化编辑器 -->
            var ue = UE.getEditor('adtips',{
                autoHeightEnabled: true,
                initialFrameHeight:400,
                toolbars: [
                    ['fullscreen', 'source', 'undo','redo','|','fontsize','horizontal','|','simpleupload','removeformat'],
                    ['bold', 'italic', 'underline', 'forecolor', 'backcolor','|','indent','|','justifyleft', 'justifyright', 'justifycenter', 'justifyjustify', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight','|','insertunorderedlist', 'insertorderedlist','|', 'imagenone','imageleft','imageright','imagecenter','|','link']
                ]
            });
            jQuery.ajaxSetup({
                case:false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            var doc = document,
                    $doc = $(doc);

            @if($good)
            ue.ready(function() {
                    ue.setContent('<?php echo $good->info ?>');
                });
            @endif

            //添加广告
            $doc.on('click','#nextid',function(){
                var title     = $('#name').val();
                var imgid     = $('#fileupload').val();
                var thumbImg1 = $('#thumb1').val();
                var thumbImg2 = $('#thumb2').val();
                var stock     = $('#stock').val();
                var express   = $('#express').val();
                var m_price   = $('#m_price').val();
                var s_price   = $('#s_price').val();
                var id        = $('#goodid').val().trim();
                var html = '';
                ue.ready(function() {
                    html = ue.getContent();
                });
                $.ajax({
                    type:"post",
                    url:'{{route('Api.Good.createGood')}}',
                    dataType:"json",
                    data:{'id':id,'name':title,'m_price':m_price,'s_price':s_price,'main_img':imgid,'thumbimg1':thumbImg1,'thumbimg2':thumbImg2,'stock':stock,'express':express,'info':html},
                    success:function(obj){
                        if(obj.level == "success"){
                            noty({text:obj.message,type:obj.level,timeout:600,
                                callback:{
                                    afterClose:function(){
                                        location.reload();
                                    }
                                }
                            });
                        }
                    },
                    error:function(obj){
                        var datajson = obj.responseJSON;
                        tusi(datajson.message);
                    }
                });
            });

            //广告缩略图上传代码
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputImage1',
                url: '{{route('Api.Upload.Image')}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '5mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png,apk"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {
                    _token: '{{ csrf_token() }}'
                },

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {

                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        $('#fileupload').val(responseObj.data.id);
                        $('#uploadthumb1').html('<span style="color: green">上传成功</span>,'+responseObj.data.img_name);
                    }
                }
            });
            uploader.init();

            //广告缩略图上传代码
            var uploader2 = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputImage2',
                url: '{{route('Api.Upload.Image')}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '5mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png,apk"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {
                    _token: '{{ csrf_token() }}'
                },

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {

                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        $('#thumb1').val(responseObj.data.id);
                        $('#uploadthumb2').html('<span style="color: green">上传成功</span>,'+responseObj.data.img_name);
                    }
                }
            });
            uploader2.init();

            //广告缩略图上传代码
            var uploader3 = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputImage3',
                url: '{{route('Api.Upload.Image')}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '5mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png,apk"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {
                    _token: '{{ csrf_token() }}'
                },

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {

                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        $('#thumb2').val(responseObj.data.id);
                        $('#uploadthumb3').html('<span style="color: green">上传成功</span>,'+responseObj.data.img_name);
                    }
                }
            });
            uploader3.init();
        });
    </script>
@endsection